package com.bizteco.policelogviewer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Properties;

import javax.crypto.Cipher;

import org.apache.log4j.Logger;

import com.bizteco.utils.CipherUtil;

public class DBConnectProperties {
	
	private Logger logger = Logger.getLogger( DBConnectProperties.class);
	
	private Properties properties = null;
	private File propsFile = null;
	private FileInputStream fis = null;
	private FileOutputStream fos = null;
	private String propsPath = System.getProperty( "user.dir") + "/.dbconnection.properties";
	
	public DBConnectProperties() throws Exception {
		this.propsFile = new File( this.propsPath);
		this.properties = new Properties();

		try {
			
			if ( !this.propsFile.exists()) {
				this.propsFile.createNewFile();
				
			}

			this.fis = new FileInputStream( this.propsFile);
			this.properties.load( this.fis);
			
		} catch (IOException e) {
			logger.error( e);
			
		}
	}

	public Properties getProperties() {
		return properties;
		
	}

	public String getProperties( String key) {
//		if( key.equals( "password")) {
//			return cipher.Decrypt( this.properties.getProperty( key));
//		} else {
//			return this.properties.getProperty( key);
//		}
		
		return this.properties.getProperty( key);
	}

	public void setProperties( Properties properties) {
		this.properties = properties;
		
	}

	public void setProperties( String path) {
		this.propsPath = path;
		
	}

	public void setProperties( String key, String value) {
//		if( key.equals( "password")) {
//			this.properties.setProperty( key, cipher.Encrypt( value));
//		} else {
//			this.properties.setProperty( key, value);
//		}
		this.properties.setProperty( key, value);
		
	}

	public void storeProperty() throws IOException {
		this.fos = new FileOutputStream( this.propsFile);
		this.properties.store( this.fos, "DB Connection");
		
	}

	public static void main(String[] arg) throws Exception {
		DBConnectProperties props = new DBConnectProperties();
		
		com.bizteco.utils.CipherUtil test4 = new CipherUtil();
//		java.security.KeyFactory.getInstance( "RSA");
		String password = "scott";
		System.out.println( "befor = " + password);
		
		System.out.println("==============");
		System.out.println( "after = " + test4.Encrypt( password));
		System.out.println( test4.Decrypt( test4.Encrypt( password)));
		
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		KeyPair keyPair = generator.generateKeyPair();
		PublicKey publicKey = keyPair.getPublic();
		PrivateKey privateKey = keyPair.getPrivate();

//		Cipher cipher = Cipher.getInstance("RSA");
//		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//		String text = "tiger";
//		byte[] t0 = text.getBytes();
////		System.out.println("P : " + ByteUtils.toHexString(t0));
//		byte[] b0 = cipher.doFinal(t0);
////		System.out.println("E : " + ByteUtils.toHexString(b0));
//		System.out.println("E : " + new String(b0));
//		cipher.init(Cipher.DECRYPT_MODE, privateKey);
//		byte[] b1 = cipher.doFinal(b0);
////		System.out.println("D : " + ByteUtils.toHexString(b1));
//		System.out.println(new String(b1));





		

	}
}
